package com.Offer;

import java.util.Arrays;
import java.util.Comparator;

/*
    面试题45：把数组排成最小的数
    题目描述：输入一个正整数数组，把数组里所有数字拼接起来排成一个数，打印 能拼接出的所有数字中最小的一个
 */
public class demo45 {
    public static void main(String[] args) {
        int[] input = {3,321,32};
        System.out.println(PrintMinNumber(input));
    }

    public static String PrintMinNumber(int[] numbers){
        if(numbers == null || numbers.length <= 0){
            return "";
        }
        int len = numbers.length;
        String[] str = new String[len];
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            str[i] = String.valueOf(numbers[i]);
        }
        Arrays.sort(str, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                String c1 = o1+o2;
                String c2 = o2+o1;
                return c1.compareTo(c2);
            }
        });

        for (int i = 0; i < len; i++) {
            sb.append(str[i]);
        }
        return sb.toString();
    }
}
